package com.tool.emulator.apple2.pers;

import com.tool.emulator.apple2.Apple2;
import com.tool.emulator.mos6502.instrs.Instr;

/**
 * VirtualPrinter.java @author tl 2011-6-28
 * <p>
 * 类似 print #6 这种，对应一个虚拟槽
 */
public class VirtualPrinter extends Peripheral {
	public static final int DEFAULT_SLOT = 3;

	public VirtualPrinter(Apple2 apple2, int slot) {
		super("VirtualPrinter", apple2, slot);
	}

	public VirtualPrinter(Apple2 apple2) {
		this(apple2, DEFAULT_SLOT);
	}

	private static char convert(int val) {
		return (char) (val & 0x7f);
	}

	@Override
	protected int[] getROM() {
		return new int[]{
				Instr.STA_ABS, 0x80 + slot * 0x10, 0xC0,
				Instr.JMP_ABS, 0xF0, 0xFD
		};
	}

	public int read_io(int addr) {
		return 0;
	}

	public synchronized boolean isActive() {
		return apple2.MEM[0x37].directGet() == 0xC0 + this.slot;
	}

	public synchronized void write_io(int addr, int val) {
		this.setAccessingTime();
		//System.err.println("printer output " + Util.b2s(val) + "\t" + convert(val));
		System.err.print(convert(val));
		System.err.flush();
	}
}
